package com.student.system.service.impl;

import com.student.system.dao.AdminDao;
import com.student.system.dao.StudentDao;
import com.student.system.dao.TeacherDao;
import com.student.system.entity.*;
import com.student.system.service.AdminService;
import com.student.system.wapper.StudentMessageWrapper;
import com.student.system.wapper.StudentRegisterWrapper;
import com.student.system.wapper.TeacherMessageWapper;
import com.student.system.wapper.TeacherRegisterWapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = new AdminDao();
    private StudentDao studentDao = new StudentDao();
    private TeacherDao teacherDao = new TeacherDao();

    @Override
    public void doLogin(HttpServletRequest req, HttpServletResponse resp) {
//
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        try {
            Admin admin= AdminDao.select(username,password);
            if(admin!=null){
                resp.getWriter().print("ok");
            }
            else {
                resp.getWriter().print("notfound");
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                resp.getWriter().print("error");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void message(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Studentmessage> list = studentDao.list();
            req.setAttribute("dataList",list);
//            req.getRequestDispatcher("/jsp/admin/main.jsp").forward(req,resp);
            req.getRequestDispatcher("/jsp/admin/Customers.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void StudentRegister(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Studentregister> studentList = studentDao.registerList();
            req.setAttribute("registerList",studentList);
            req.getRequestDispatcher("/jsp/admin/StudentRegister.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void teacherMessage(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Teachermessage> list = teacherDao.messageList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/jsp/admin/TeacherMessage.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void teacherRegister(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<Teacherregister> list = teacherDao.teacherRegisterList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/jsp/admin/TeacherRegister.jsp").forward(req,resp);
        } catch (Exception throwables) {
           throwables.printStackTrace();
        }
    }

    @Override
    public void StudentMessageEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String id = req.getParameter("id");
        Studentmessage studentmessage = studentDao.selectById(Integer.parseInt(id));
        req.setAttribute("data",studentmessage);
        req.getRequestDispatcher("/jsp/admin/StudentMessageEdit.jsp").forward(req,resp);
    }

    @Override
    public void doEditStudentMessage(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        Studentmessage studentmessage = StudentMessageWrapper.wrapper(req);
        studentDao.doEditStudentMessage(studentmessage);
        resp.getWriter().print("ok");
    }

    @Override
    public void StudentRegisterEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String id = req.getParameter("id");
        Studentregister studentregister = studentDao.selectRegisterById(Integer.parseInt(id));
        req.setAttribute("data",studentregister);
        req.getRequestDispatcher("/jsp/admin/StudentRegisterEdit.jsp").forward(req,resp);
    }

    @Override
    public void doEditStudentRegister(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        Studentregister studentregister = StudentRegisterWrapper.registerWrapper(req);
        studentDao.doEditStudentRegister(studentregister);
        resp.getWriter().print("ok");
    }

    @Override
    public void TeacherRegisterEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String id = req.getParameter("id");
        Teacherregister teacherregister = teacherDao.selectTeacherRegisterById(Integer.parseInt(id));
        req.setAttribute("data",teacherregister);
        req.getRequestDispatcher("/jsp/admin/TeacherRegisterEdit.jsp").forward(req,resp);
    }

    @Override
    public void doEditTeacherRegister(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        Teacherregister teacherregister = TeacherRegisterWapper.teacherRegisterWrapper(req);
        teacherDao.doEditTeacherRegister(teacherregister);
        resp.getWriter().print("ok");
    }

    @Override
    public void TeacherMessageEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException, ServletException {
        String id = req.getParameter("id");
        Teachermessage teachermessage = teacherDao.selectTeacherMessageById(Integer.parseInt(id));
        req.setAttribute("data",teachermessage);
        req.getRequestDispatcher("/jsp/admin/TeacherMessageEdit.jsp").forward(req,resp);
    }

    @Override
    public void doEditTeacherMessage(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        Teachermessage teachermessage = TeacherMessageWapper.teacherMessageWrapper(req);
        teacherDao.doEditTeacherMessage(teachermessage);
        resp.getWriter().print("ok");
    }

}
